
<div class="wrapper wrapper-content animated page-heading fadeInRight">
    <div class="row">
        <div class="col-lg-9">
            <div class="tabs-container">
                <ul class="nav nav-tabs" id="tab-list-2">
                    <li class="links active" id="search-link"><a href="#search" role="tab" data-toggle="tab">Search</a></li>
                </ul>
                <div class="tab-content">
                   <div id="search" class="tab-pane active">
                        <div class="panel-body">
                          <div class="row animate fadeIn">
                              <form id="ubs_search_mtr" class="form-horizontal">
                                  <div class="ibox-title" style="border:none;"><h5 style="color: #00afef"><i>Search</i></h5>
                                  </div>
                                     <div class="ibox-content">
                                          <div class="form-group"><label class="col-lg-4 control-label">Account</label>
                                              <div class="col-lg-5">
                                                <select style="" id="" class="chosen-select" name="acct">
                                                    <?php foreach ($accounts as $acct) { ?>
                                                    <option value="<?php echo $acct->account_id; ?>" ><?php echo $acct->acct_num.' - '.$acct->acct_name; ?></option>
                                                    <?php } ?>
                                                </select>
                                              </div>
                                          </div>
                                          <div class="form-group">
                                              <label class="col-lg-4 control-label">Year-Month</label>
                                              <div class="col-lg-5">
                                                  <select style="" id="" class="chosen-select" name="ubs_ym">
                                                    <?php for($i = date('Y') ; $i > 2000 ; $i--) { ?>
                                                      <?php  for ($m = 12; $m > 0 ; $m--) { ?>
                                                       <option value="<?php echo $i.'-'.($m < 10 ? '0'.$m : $m); ?>" ><?php echo date('M',strtotime('01-'.$m.'-1970')).'-'.date('Y' , strtotime('01-01-'.$i)); ?></option> 
                                                    <?php  } } ?>
                                                </select>
                                              </div>
                                          </div>
                                           <div class="form-group">
                                              <label class="col-lg-4 control-label"></label>
                                              <div class="col-lg-5">
                                                  <a href="javascript:document.getElementById('searchForm_rate').reset();" class="btn btn-warning btn-md pull-right m-t-n-xs clear-btn" id="search-form"> Clear</a>
                                                  <button id="ubs-search-btn" class="btn btn-primary btn-md pull-right m-t-n-xs search-btn btn-margin-right" > Search</button>
                                              </div>
                                          </div>
                                      </div>
                                  </form>
                          </div>
                          <div class="row">
                              <div class="col-lg-2">
                                  <button id = "adds" class="btn btn-success btn-md m-t-n-xs add-tab-btn" type="button"> Add</button>
                              </div>
                          </div>
                          <div class="" id="searchResult" >

                                  <div class="ibox-content custom">
                                       <div class="table-responsive">
                                          <table class="table-bordered table-compact" style="width:100%;">
                                              <thead>
                                                  <tr>
                                                      <th style="padding:8px;text-align:center;">Invoice Num</th>
                                                      <th style="padding:8px;text-align:center;">Reading Date</th>
                                                      <th style="padding:8px;text-align:center;">Account No.</th>
                                                      <th style="padding:8px;text-align:center;">Account Name</th>
                                                      <th style="padding:8px;text-align:center;width:75px;">Action</th>
                                                  </tr>
                                              </thead>
                                              <tbody id="resultfound" >

                                              </tbody>
                                          </table>
                                        </div>
                                  </div>
                              </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div><!-- END of class row-->
</div>
<input type="hidden" id="count_add" value="0">
<input type="hidden" id="count_edit" value="0">
<input type="hidden" id="count_view" value="0">
<script>
$(document).ready(function(){
  $(".chosen-select").chosen({width:"100%;"});
        var count_edit  =0;
        var count_view  =0;
        $('#adds').click(function(){
            $('#notify').text("");
                if($('#count_add').val() == 0){
                    $('#tab-list-2').append('<li id="add-link" class="links"><a id="2nd" data-toggle="tab" href="#add">Add<button id="addX" style="margin-left:30px;" class="close" type="button" title="Remove this page">×</button></a></li>');
                    $('.tab-content').append('<div id="add" class="tab-pane"><div class="panel-body"><div id="change2"><div class="sk-spinner sk-spinner-double-bounce"><div class="sk-double-bounce1"></div><div class="sk-double-bounce2"></div></div></div></div></div>');
                    $("#change2").load("<?php echo $this->session->userdata('forload') . '/add_form'; ?>")
                    $('#count_add').val('1');
                    add_action('show'); search_action('close');
                }

          });
        $("#ubs-search-btn").click(function(e) {
            e.preventDefault();
            var trHTML='';
            console.log('hi');
            $('#resultfound').empty();
            $('#searchResult').show();
              $.ajax({
                type: "POST",
                url: "<?php echo base_url($this->session->userdata('forajax') . '/search_ubs_mtr'); ?>",
                cache: false,
                dataType: 'json',
                data: $('#ubs_search_mtr').serialize(),
                success: function(e) {
                    console.log(e);
                    if(e == "No results found."){
                        console.log('error');
                        $('#resultfound').append("<td colspan='5' align='center'><h3>No Result Found</h3></td>");
                        notifymsg("danger","No Result Found");
                    }else{
                        console.log(e.r2);
                        console.log(e.r1);
                        var countData = 0;
                        var edit = '<button style="margin-left:2px;margin-bottom:1px;" class="btn btn-sm btn-outline btn-info btn-xs edit" title="edit"><i class="fa fa-pencil fa-lg"></i></button>';
                        $.each(e.r1, function(i, item) {
                            trHTML += '<tr style="line-height:0;padding:2px;"><input id="account_id" type="hidden" value="'+ e.r1[i].account_id +'"><input id="read_id" type="hidden" value="'+ e.r1[i].reading_id +'"><td style="text-align:left; padding:5px;">'+ e.r1[i].bill_id +'</td><td style="text-align:left; padding-left:5px;">'+ e.r1[i].reading_date +'</td><td style="text-align:left; padding-left:5px;">'+ e.r1[i].acct_num +'</td><td style="text-align:left; padding-left:5px;">'+ e.r1[i].acct_name +'</td><td align="center" style="text-align:center; padding:2px;">'+(e.r2 == true ? '' : edit)+'<button style="margin-left:2px;margin-bottom:1px;" class="btn btn-sm btn-outline btn-info btn-xs view" title="View"><i class="fa fa-file-text-o fa-lg"></i></button></td></tr>';
                            countData++;
                        });
                        $('#resultfound').append(trHTML);
                        notifymsg("success","   . . . Search Success  , Found("+countData+") Item(s)");
                    }
                },
                complete: function(data) {
                      $('#search-btn').removeAttr('disabled');
                },
                beforeSend: function(){
                      $('#search-btn').attr('disabled',true);
                }
              });
            });


        // $(document).on('click','.btn.btn-sm.btn-outline.btn-info.btn-xs.edit',function(){
        //   close_edit();
        //     var id         = $(this).closest('tr').children('#rate_id').val();
        //     if($('#count_edit').val() == 0){
        //         $('#tab-list-2').append('<li id="edit-link" class="links"><a data-toggle="tab" href="#edit">Edit<button id="editX" style="margin-left:30px;" class="close" type="button" title="Remove this page">×</button></a></li>');
        //         $('.tab-content').append('<div id="edit" class="tab-pane"><div class="panel-body"><div id="change3"></div></div></div>');
        //         $("#count_edit").val('1');
        //        }
        //     $("#change3").load("<?php echo $this->session->userdata('forload') . '/edit_form/'; ?>"+id)
        //     edit_action('show'); search_action('close');
        //     notifymsg("hide","hide");
        // });
        $(document).on('click','.btn.btn-sm.btn-outline.btn-info.btn-xs.view',function(){
            var id         = $(this).closest('tr').children('#read_id').val();
            if($('#count_view').val() == 0){
                $('#tab-list-2').append('<li id="view-link" class="links"><a data-toggle="tab" href="#view">View<button id="viewX" style="margin-left:30px;" class="close" type="button" title="Remove this page">×</button></a></li>');
                $('.tab-content').append('<div id="view" class="tab-pane"><div class="panel-body"><div id="change4"></div></div></div>');
                $("#count_view").val('1');
                }
            $("#change4").load("<?php echo $this->session->userdata('forload') . '/view_form/'; ?>"+id)
            view_action('show'); search_action('close');
            notifymsg("hide","hide");
        });

        $(document).on('click','.btn.btn-sm.btn-outline.btn-info.btn-xs.edit',function(){
            var id         = $(this).closest('tr').children('#read_id').val();
            if($('#count_edit').val() == 0){
                $('#tab-list-2').append('<li id="edit-link" class="links"><a data-toggle="tab" href="#edit">Edit<button id="editX" style="margin-left:30px;" class="close" type="button" title="Remove this page">×</button></a></li>');
                $('.tab-content').append('<div id="edit" class="tab-pane"><div class="panel-body"><div id="change3"></div></div></div>');
                $("#count_edit").val('1');
                }
            $("#change3").load("<?php echo $this->session->userdata('forload') . '/edit_form/'; ?>"+id)
            edit_action('show'); search_action('close');
            notifymsg("hide","hide");
        });
});
</script>